import React from 'react'

class Child extends React.Component{
  state={
    name:'儿子的信',
    inputVal:''
  }
  reTest(e){
    this.setState({inputVal: e.target.value.replace(/[^\d{1,}.\d{1,}|\d{1,}]/g,'')});
    console.log(this.state.inputVal)
  }
  render(){
    return(
      <div>
        {/* onChange = {this.props.handleInputVal} */}
        <input type='number' onInput={this.reTest.bind(this)} />
        <div>
          {this.state.inputVal}
        </div>
      </div>
    )
  }
}
class Login extends React.Component {
  state = {
    inputVal:''
  };

  home(){
    this.props.history.push({pathname:"/home/2"});
  }
  jumpquery(){
    this.props.history.push({pathname:"/jumptest",query: { name : 'sunny' }})
  }
  NumberCheck(num){
    var str = num;
    var len1 = str.substr(0, 1);
    var len2 = str.substr(1, 1);
    //如果第一位是0，第二位不是点，就用数字把点替换掉
    if (str.length > 1 && len1 === 0 && len2 !== ".") {
      str = str.substr(1, 1);
    }
    //第一位不能是.
    if (len1 === ".") {
      str = "";
    }
    //限制只能输入一个小数点
    if (str.indexOf(".") !== -1) {
      var str_ = str.substr(str.indexOf(".") + 1);
      if (str_.indexOf(".") !== -1) {
        str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
      }
    }
    //正则替换，保留数字和小数点
    str = str.replace(/[^\d^.]+/g,'')
    //如果需要保留小数点后两位，则用下面公式
    str = str.replace(/\.\d\d$/,'')
    return str;
  }
  handleInputVal(event){
    event.target.value = this.NumberCheck(event.target.value)
    this.setState({inputVal: event.target.value});
  }
  render() {
    return (
      <div>
        爸爸在此：
        <Child handleInputVal={this.handleInputVal.bind(this)}></Child>
        儿子的值：{this.state.inputVal}
        <br/>
        <a href='/home/2'>home</a>

        <button onClick={this.home.bind(this)}>home</button>
        <button onClick={this.jumpquery.bind(this)}>jumpquery</button>
      </div>
    );
  }
}
export default Login
